package cn.chenhao.user.controller;

import cn.chenhao.basic.exception.BusinessException;
import cn.chenhao.basic.util.AjaxResult;
import cn.chenhao.user.dto.LoginDto;
import cn.chenhao.user.service.ILoginService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(tags = "后台登录接口",description = "后台登录接口详细描述")
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @PostMapping("/account")
    public AjaxResult loginAccount(@RequestBody LoginDto loginDto){
        try {
            Map<String, Object> map = loginService.loginAccount(loginDto);
            return AjaxResult.me().setResultObj(map);
        }
        catch (BusinessException exception) {
            exception.printStackTrace();
            return AjaxResult.me().setMessage("登录失败，晚哈儿再来！");
        }
        catch (Exception exception) {
            exception.printStackTrace();
            return AjaxResult.me().setMessage("系统扯拐，不关程序员的事！");
        }
    }

    /**
     * 微信登录接口
     * @param map
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody Map<String,String> map){
        try {
            return loginService.wechat(map);
        }
        catch (BusinessException exception) {
            exception.printStackTrace();
            return AjaxResult.me().setMessage("登录失败，晚哈儿再来！");
        }
        catch (Exception exception) {
            exception.printStackTrace();
            return AjaxResult.me().setMessage("系统扯拐，不关程序员的事！");
        }
    }


    /**
     * 微信绑定接口
     * @param params
     * @return
     */
    @PostMapping("/binder/wechat")
    public AjaxResult binder(@RequestBody Map<String,String> params){
        try {
            Map<String,Object> map = loginService.binder(params);
            // 需要service返回token字符串和登录人信息
            return AjaxResult.me().setResultObj(map);
        }
        catch (BusinessException exception) {
            exception.printStackTrace();
            return AjaxResult.me().setMessage("登录失败，晚哈儿再来！");
        }
        catch (Exception exception) {
            exception.printStackTrace();
            return AjaxResult.me().setMessage("系统扯拐，不关程序员的事！");
        }
    }



}
